package com.liuguang.framework.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Build.VERSION;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

import androidx.annotation.ColorInt;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class StatusBarUtils {
    private static final int EXPAND_NAV = 768;
    private static final int EXPAND_STATUS = 1280;
    private static final int STABLE_NAV = 4866;
    private static final int STABLE_STATUS = 5380;
    private static final int STATUSBAR_TYPE_ANDROID6 = 3;
    private static final int STATUSBAR_TYPE_DEFAULT = 0;
    private static final int STATUSBAR_TYPE_FLYME = 2;
    private static final int STATUSBAR_TYPE_MIUI = 1;
    private static final int UNSTABLE_NAV = 2;
    private static final int UNSTABLE_STATUS = 4;
    private static boolean isOpenMonLayer;
    private static int mType;

    @Retention(RetentionPolicy.SOURCE)
    private @interface StatusBarType {
    }

    private static boolean supportTranslucent() {
        return VERSION.SDK_INT >= 19 && !Build.BRAND.toLowerCase().contains("essential");
    }

    public static void transparencyBar(Activity activity) {
        transparencyBar(activity, 1073741824);
    }

    public static void transparencyBar(Activity activity, boolean z) {
        isOpenMonLayer = z;
        transparencyBar(activity, 1073741824);
    }

    @TargetApi(19)
    public static void transparencyBar(Activity activity, @ColorInt int i) {
        if (supportTranslucent()) {
            if ((SystemUtils.isMeizu() || SystemUtils.isMIUI()) && !isOpenMonLayer) {
                activity.getWindow().setFlags(67108864, 67108864);
                return;
            }
            if (VERSION.SDK_INT >= 21) {
                Window window = activity.getWindow();
                window.getDecorView().setSystemUiVisibility(EXPAND_STATUS);
                if (isOpenMonLayer) {
                    window.clearFlags(67108864);
                    window.addFlags(Integer.MIN_VALUE);
                    window.setStatusBarColor(i);
                } else if (VERSION.SDK_INT < 23 || !supportTransclentStatusBar6()) {
                    window.clearFlags(67108864);
                    window.addFlags(Integer.MIN_VALUE);
                    window.setStatusBarColor(i);
                } else {
                    window.clearFlags(67108864);
                    window.addFlags(Integer.MIN_VALUE);
                    window.setStatusBarColor(0);
                }
            }
        }
    }

    public static void OpenMonLayerMode(boolean z) {
        isOpenMonLayer = z;
    }

    public static boolean setStatusBarIconDark(Activity activity) {
        if (isOpenMonLayer || activity == null || SystemUtils.isZTKC2016()) {
            return false;
        }
        if (mType != 0) {
            return setStatusBarLightMode(activity, mType);
        }
        if (VERSION.SDK_INT >= 19) {
            if (isMIUICustomStatusBarLightModeImpl() && MIUISetStatusBarLightMode(activity.getWindow(), true)) {
                mType = 1;
                return true;
            } else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
                mType = 2;
                return true;
            } else if (VERSION.SDK_INT >= 23) {
                Android6SetStatusBarLightMode(activity.getWindow(), true);
                mType = 3;
                return true;
            } else if (VERSION.SDK_INT == 21 || VERSION.SDK_INT == 22) {
                transparencyBar(activity, false);
                return true;
            } else {
                activity.getWindow().addFlags(67108864);
                SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
                systemBarTintManager.setStatusBarTintColor(-16777216);
                systemBarTintManager.setStatusBarTintEnabled(true);
            }
        }
        return false;
    }

    public static boolean setStatusBarIconLight(Activity activity) {
        if (isOpenMonLayer || activity == null) {
            return false;
        }
        if (mType == 0) {
            return true;
        }
        if (mType == 1) {
            return MIUISetStatusBarLightMode(activity.getWindow(), false);
        }
        if (mType == 2) {
            return FlymeSetStatusBarLightMode(activity.getWindow(), false);
        }
        if (mType == 3) {
            return Android6SetStatusBarLightMode(activity.getWindow(), false);
        }
        return true;
    }

    public static int getStatusbarType() {
        return mType;
    }

    private static boolean isMIUICustomStatusBarLightModeImpl() {
        boolean z = true;
        if (SystemUtils.isMIUIV9() && VERSION.SDK_INT < 23) {
            return true;
        }
        if (!SystemUtils.isMIUIV5() && !SystemUtils.isMIUIV6() && !SystemUtils.isMIUIV7() && !SystemUtils.isMIUIV8()) {
            z = false;
        }
        return z;
    }

    private static boolean setStatusBarLightMode(Activity activity, int i) {
        if (i == 1) {
            return MIUISetStatusBarLightMode(activity.getWindow(), true);
        }
        if (i == 2) {
            return FlymeSetStatusBarLightMode(activity.getWindow(), true);
        }
        if (i == 3) {
            return Android6SetStatusBarLightMode(activity.getWindow(), true);
        }
        return false;
    }

    @TargetApi(23)
    private static boolean Android6SetStatusBarLightMode(Window window, boolean z) {
        window.getDecorView().setSystemUiVisibility(changeStatusBarModeRetainFlag(window, z ? 8192 : 256));
        if (SystemUtils.isMIUIV9()) {
            MIUISetStatusBarLightMode(window, z);
        }
        return true;
    }

    @TargetApi(23)
    private static int changeStatusBarModeRetainFlag(Window window, int i) {
        return retainSystemUiFlag(window, retainSystemUiFlag(window, retainSystemUiFlag(window, retainSystemUiFlag(window, retainSystemUiFlag(window, retainSystemUiFlag(window, i, 1024), 4), 2), 4096), 1024), 512);
    }

    public static int retainSystemUiFlag(Window window, int i, int i2) {
        return (window.getDecorView().getSystemUiVisibility() & i2) == i2 ? i | i2 : i;
    }

    public static boolean FlymeSetStatusBarLightMode(Window window, boolean z) {
        if (window != null) {
            Android6SetStatusBarLightMode(window, z);
            try {
                LayoutParams attributes = window.getAttributes();
                Field declaredField = LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field declaredField2 = LayoutParams.class.getDeclaredField("meizuFlags");
                declaredField.setAccessible(true);
                declaredField2.setAccessible(true);
                int i = declaredField.getInt(null);
                int i2 = declaredField2.getInt(attributes);
                declaredField2.setInt(attributes, z ? i2 | i : (i ^ -1) & i2);
                window.setAttributes(attributes);
                return true;
            } catch (Exception unused) {
            }
        }
        return false;
    }

    public static boolean MIUISetStatusBarLightMode(Window window, boolean z) {
        if (window != null) {
            Class cls = window.getClass();
            try {
                Class cls2 = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                int i = cls2.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE").getInt(cls2);
                Method method = cls.getMethod("setExtraFlags", new Class[]{Integer.TYPE, Integer.TYPE});
                if (z) {
                    method.invoke(window, new Object[]{Integer.valueOf(i), Integer.valueOf(i)});
                    return true;
                }
                method.invoke(window, new Object[]{Integer.valueOf(0), Integer.valueOf(i)});
                return true;
            } catch (Exception unused) {
            }
        }
        return false;
    }

    public static boolean supportTransclentStatusBar6() {
        return !SystemUtils.isZUKZ1() && !SystemUtils.isZTKC2016();
    }

    public static void hideUnStableStatusBar(Activity activity) {
        setFlag(activity, 4);
    }

    public static void showUnStableStatusBar(Activity activity) {
        clearFlag(activity, 4);
    }

    public static void hideUnStableNavBar(Activity activity) {
        setFlag(activity, 2);
    }

    public static void showUnStableNavBar(Activity activity) {
        clearFlag(activity, 2);
    }

    public static void hideStableStatusBar(Activity activity) {
        setFlag(activity, STABLE_STATUS);
    }

    public static void showStableStatusBar(Activity activity) {
        clearFlag(activity, STABLE_STATUS);
    }

    public static void hideStableNavBar(Activity activity) {
        setFlag(activity, STABLE_NAV);
    }

    public static void showStableNavBar(Activity activity) {
        clearFlag(activity, STABLE_NAV);
    }

    public static void expandStatusBar(Activity activity) {
        setFlag(activity, EXPAND_STATUS);
    }

    public static void expandNavBar(Activity activity) {
        setFlag(activity, EXPAND_NAV);
    }

    public static void setStatusBar(Activity activity, int i) {
        if (VERSION.SDK_INT >= 23) {
            if (VERSION.SDK_INT >= 24) {
                try {
                    Field declaredField = Class.forName("com.android.internal.policy.DecorView").getDeclaredField("mSemiTransparentStatusBarColor");
                    declaredField.setAccessible(true);
                    declaredField.setInt(activity.getWindow().getDecorView(), 0);
                } catch (Exception unused) {
                }
            }
            Window window = activity.getWindow();
            window.addFlags(Integer.MIN_VALUE);
            window.clearFlags(67108864);
            window.setStatusBarColor(i);
        }
    }

    public static void transparentStatusBar(Activity activity) {
        if (VERSION.SDK_INT >= 21) {
            Window window = activity.getWindow();
            window.addFlags(Integer.MIN_VALUE);
            window.addFlags(67108864);
            window.setStatusBarColor(0);
            if (VERSION.SDK_INT >= 24) {
                try {
                    Field declaredField = Class.forName("com.android.internal.policy.DecorView").getDeclaredField("mSemiTransparentStatusBarColor");
                    declaredField.setAccessible(true);
                    declaredField.setInt(window.getDecorView(), 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @SuppressLint("ResourceType")
    public static void transparentNavBar(Activity activity) {
        if (VERSION.SDK_INT >= 21) {
            expandNavBar(activity);
            activity.getWindow().setNavigationBarColor(activity.getResources().getColor(17170445));
        }
    }

    public static void setFlag(Activity activity, int i) {
        if (VERSION.SDK_INT >= 19) {
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility(i | decorView.getSystemUiVisibility());
        }
    }

    public static void clearFlag(Activity activity, int i) {
        if (VERSION.SDK_INT >= 19) {
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility((i ^ -1) & decorView.getSystemUiVisibility());
        }
    }

    public static void setToggleFlag(Activity activity, int i) {
        if (VERSION.SDK_INT < 19) {
            return;
        }
        if (isFlagUsed(activity, i)) {
            clearFlag(activity, i);
        } else {
            setFlag(activity, i);
        }
    }

    public static boolean isFlagUsed(Activity activity, int i) {
        return (activity.getWindow().getDecorView().getSystemUiVisibility() & i) == i;
    }
}
